package com.hanxiaozhang.system.controller;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.hanxiaozhang.annotation.EnableGlobalException;
import com.hanxiaozhang.result.Result;
import com.hanxiaozhang.system.dao.RoleDao;
import com.hanxiaozhang.system.dto.RoleDTO;
import com.hanxiaozhang.util.PageUtil;
import com.hanxiaozhang.validation.util.ValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanxiaozhang.system.entity.RoleEntity;
import com.hanxiaozhang.system.service.RoleService;

import javax.annotation.Resource;


/**
 * 角色表
 *
 * @author hanxiaozhang
 * @email hanxiaozhang2018@sina.com
 * @date 2021-03-31
 */
@Slf4j
@Controller
@RequestMapping("/role")
@EnableGlobalException
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Resource
    private RoleDao roleDao;

    /**
     * 查询列表
     *
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('system:role:list')")
    public Result<PageUtil> list(@RequestParam Map<String, Object> params) {

        params.put("offset", (Integer.parseInt(params.get("current").toString())-1));
        params.put("limit", Integer.parseInt(params.get("pageSize").toString()));
        params.remove("current");
        params.remove("pageSize");
        List<RoleDTO> roleList = roleService.list(params);
        int total = roleDao.count(params);
        return Result.success(new PageUtil(roleList, total));
    }


    /**
     * 保存
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('system:role:save')")
    public Result<Void> save(RoleDTO role) {
        valid(role, true);
        roleService.save(role);
        return Result.success();
    }


    /**
     * 修改
     *
     * @param role
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('system:role:update')")
    public Result<Void> update(RoleDTO role) {
        valid(role, false);
        roleService.update(role);
        return Result.success();
    }

    /**
     * 参数校验
     *
     * @param role
     * @param saveFlag
     */
    private void valid(RoleDTO role, boolean saveFlag) {
        ValidationUtil.checkParameter(ValidationUtil.checkObjectIsNotNull(role.getRoleName()), "角色名称不能为空");
        if (saveFlag) {
            RoleDTO oldRole = roleDao.getByRoleName(role.getRoleName());
            ValidationUtil.checkParameter(oldRole == null, "岗位名称重复");
        } else {
            RoleDTO oldRole = roleDao.getByRoleNameAndNoRoleId(role.getRoleName(), role.getRoleId());
            ValidationUtil.checkParameter(oldRole == null, "岗位名称重复");
        }
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:role:remove')")
    public Result<Void> remove(Long roleId) {

        return roleService.remove(roleId);
    }


    /**
     * 批量删除
     *
     * @param roleIds
     * @return
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @PreAuthorize("hasAuthority('system:role:batchRemove')")
    public Result<Void> remove(Long[] roleIds) {

        return roleService.batchRemove(roleIds);
    }

}
